package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.AdminAreaBean;
import com.koron.css2.serviceManage.bean.AdminAreaQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.systemManage.servlet.DeleteRole;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 区域删除
 *
 * @author lwc
 */
public class AdminAreaDelete implements ServerInterface {
    private static Logger log = LogManager.getLogger(DeleteRole.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<String> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);
        try {
            AdminAreaQueryBean bean = JsonUtils.objectToPojo(req.getData(), AdminAreaQueryBean.class);
            if (StringUtils.isEmpty(bean.getAreaNo())) {
                msg.setCode(Constant.MESSAGE_INT_FAIL);
                msg.setDescription("参数不能为空");
                return msg;
            }
            PubAdminAreaMapper mapper = factory.getMapper(PubAdminAreaMapper.class);
            List<AdminAreaBean> adminAreaBeans = mapper.listPaging(bean);
            if (adminAreaBeans.size() > 0) {
                msg.setCode(Constant.MESSAGE_INT_FAIL);
                msg.setDescription("请先删除下级区域");
                return msg;
            }
            mapper.delAdminAreaByAreaNo(bean.getAreaNo());
        } catch (Exception e) {
            log.error("区域删除出错：" + e);
            msg.setCode(Constant.MESSAGE_INT_FAIL);
            msg.setDescription("区域删除出错");
        }
        return msg;
    }
}
